package com.atguigu.ssyx.service;

import com.atguigu.ssyx.api.ZhgjApi;
import com.atguigu.ssyx.model.ZhgjResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class CheckService {

    @Autowired
    private ZhgjApi zhgjApi;
    @Autowired
    private OpenGateService openGateService;

    public ZhgjResponse openGateByQrCode(String qrCodeUrl, Integer sn, Integer gateNo) {
        // 调用二维码核验接口获取预约记录
        ZhgjResponse qrCodeResult = zhgjApi.checkByQrCode(qrCodeUrl);
        // 判断二维码核验结果
        zhgjApi.judgeStatus(qrCodeResult);
        // 获取预约记录及其id
        Map<String, Object> visit = (Map<String, Object>) qrCodeResult.getData();
        String id = (String) visit.get("id");
        //调用到访确认接口
        ZhgjResponse visitResult = zhgjApi.checkVisit(id);
        //判断到访确认结果
        if(zhgjApi.judgeStatus(visitResult) == true) {
            //判断到访状态1和0
            Map<String, Object> visitStatus = (Map<String, Object>) visitResult.getData();
            if ("1".equals(visitStatus.get("state"))) {
                // 到访确认成功，调用闸机接口进行开门
                openGateService.openGate(sn, gateNo);
            }
        }
        return qrCodeResult;
    }

}
